package com.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.LoginCreds;
import com.bean.ZjMusic;
import com.bean.other.MetaInfo;
import com.bean.realation.MusicCollect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ZjMusicMapper;
import com.mapper.repository.ZjMusicRepository;
import com.mapper.repository.relation.MusicCollectRepository;
import com.service.impl.ZjMusicService;
import com.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class ZjMusicServiceImpl implements ZjMusicService {

    @Autowired
    private ZjMusicRepository zjMusicRepository;

    @Autowired
    private ZjMusicMapper zjMusicMapper;

    @Override
    public String addMusicFromApi(ZjMusic zjMusic) {
        //判断是否已经存在，如果存在就不要再保存了
        int count = zjMusicRepository.countByNameAndUrl(zjMusic.getName(),zjMusic.getUrl());
        if(count>0){
            return "当前音乐已保存";
        }
        zjMusic.setId(UUID.randomUUID().toString());
        zjMusic.setStatus("200");
        zjMusic.setCreatetime(LocalDateTime.now());
        zjMusicRepository.save(zjMusic);
        return "保存成功";
    }

    @Override
    public ZjMusic randomMusic(String userid) {
        ZjMusic zjMusic = zjMusicMapper.randomZjMusic(userid);
        return zjMusic;
    }

    @Override
    public JSONObject musicStatus(ZjMusic zjMusic) {
        JSONObject jsonObject =new JSONObject();
        String userid = zjMusic.getUserid();
        int count = zjMusicRepository.countByNameAndUrl(zjMusic.getName(),zjMusic.getUrl());
        if(count>0){
            jsonObject.put("save","已保存");
            zjMusic = zjMusicRepository.findByNameAndUrl(zjMusic.getName(),zjMusic.getUrl());
            count = musicCollectRepository.countByUseridAndMusicid(userid,zjMusic.getId());
            if(count>0){
                jsonObject.put("collect","已收藏");
            }else{
                jsonObject.put("collect","未收藏");
            }
        }else{
            jsonObject.put("save","未保存");
            jsonObject.put("collect","未收藏");
        }

        return jsonObject;
    }

    @Autowired
    private MusicCollectRepository musicCollectRepository;

    @Override
    public String collect(ZjMusic zjMusic) {
        MusicCollect musicCollect =new MusicCollect();
        musicCollect.setId( UUID.randomUUID().toString());
        musicCollect.setUserid(zjMusic.getUserid());

        int count = zjMusicRepository.countByNameAndUrl(zjMusic.getName(),zjMusic.getUrl());
        if(count>0){
            zjMusic = zjMusicRepository.findByNameAndUrl(zjMusic.getName(),zjMusic.getUrl());
            musicCollect.setMusicid(zjMusic.getId());
        }else{
            zjMusic.setId(UUID.randomUUID().toString());
            zjMusic.setStatus("200");
            zjMusic.setCreatetime(LocalDateTime.now());
            zjMusicRepository.save(zjMusic);
            musicCollect.setMusicid(zjMusic.getId());
        }
        musicCollectRepository.save(musicCollect);
        return "收藏成功";
    }

    @Autowired
    private ApiInfoServiceImpl apiInfoService;

    @Override
    public List<ZjMusic> checkMusic(ZjMusic zjMusicParam) {
        //String keyword = "落了白";
        String token = "LwExDtUWhF3rH5ib";
        //String url = "https://v2.alapi.cn/api/music/search?keyword=%s&token=%s";
        String url = "https://v2.alapi.cn/api/music/search?keyword=%s&token=%s&offset=%d&limit=%d";
        //url = String.format(url,zjMusic.getName(),token);
        url = String.format(url,zjMusicParam.getName(),token,zjMusicParam.getOffset(),zjMusicParam.getLimit());
        String jsonObject = apiInfoService.webClientBuilderGets(url);
        JSONObject result = JSONObject.parseObject(jsonObject);

        List<ZjMusic> list = new ArrayList<>();
        JSONArray song = result.getJSONObject("data").getJSONArray("songs");
        for(int i= 0;i<song.size();i++){
            JSONObject songObject = song.getJSONObject(i);
            ZjMusic zjMusic = new ZjMusic() ;
            zjMusic.setDuration(TimeUtil.duration(songObject.getLong("duration")));
            zjMusic.setName(songObject.getString("name"));
            zjMusic.setPicurl(songObject.getJSONArray("artists").getJSONObject(0).getString("img1v1Url"));
            zjMusic.setArtistsname(songObject.getJSONArray("artists").getJSONObject(0).getString("name"));
            String jxUrl = "https://api.oick.cn/wyy/api.php?id=%s";
            jxUrl = String.format(jxUrl,songObject.getString("id"));
            String jxResult = apiInfoService.webClientBuilderGets2(jxUrl);
            if(jxResult!=null)
                zjMusic.setUrl(jxResult);
                list.add(zjMusic);
        }
        return list;
    }

    @Override
    public PageInfo<ZjMusic> select(ZjMusic zjMusic, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<ZjMusic> list=zjMusicMapper.selectLike(zjMusic);
        PageInfo<ZjMusic> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String delZjMusic(String id) {
        zjMusicRepository.deleteById(id);
        return "成功";
    }

    //========================================

    @Override
    public PageInfo<ZjMusic> listSelfMusic(ZjMusic zjMusic, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<ZjMusic> list=zjMusicMapper.listSelfMusic(zjMusic);
        PageInfo<ZjMusic> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String delSelfMusic(MusicCollect musicCollect) {
        zjMusicMapper.deleteSelfMusic(musicCollect);
        return "成功";
    }
}
